package com.robmayhew.jsc;

import java.io.File;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Rob
 * Date: 5-Apr-2005
 * Time: 8:07:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class JavaSrcWordTokenizer
{
    private String[] src = null;
    private boolean inComment = false;
    private boolean inString = false;
    private JavaSrcTokenList tokens = new JavaSrcTokenList();

    public JavaSrcWordTokenizer(String[] src)
    {
        this.src = src;
        build();
    }

    public JavaSrcWordTokenizer(String src)
    {
        this.src = new String[]{src};
        build();
    }

    public String[] tokenize()
    {

        String[] result = new String[0];
        return result;
    }

    public JavaSrcTokenList getTokens()
    {
        return tokens;
    }

    private void build()
    {
        int row = 0;
        int col = 0;
        int pos = 0;
        StringBuffer currentWord = new StringBuffer();
        boolean newWord = false;
        for (row = 0; row < src.length; row++)
        {

            char[] caTemp = src[row].toCharArray();
            char[] ca = new char[caTemp.length + 1];
            System.arraycopy(caTemp, 0, ca, 0, caTemp.length);
            ca[caTemp.length] = ' '; // Strange things happen when a line ends with a non character.
                                     // adding a character will fix this
            newWord = true;
            for (col = 0; col < ca.length; col++)
            {


                if (newWord)
                {
                    int p = pos -1;
                    tokens.add(row, col, currentWord.toString(), src[row], p);
                    currentWord = new StringBuffer();
                    newWord = false;
                }
                char c = ca[col];

                if (isLetter(c))
                {                      
                    // Add to the current Word
                    currentWord.append(c);
                }
                else
                {
                    newWord = true;

                }
                pos++;
            }
        }
        row--;
        if(row < 0)
            row = 0;

        if(src.length > 0 && currentWord.length() > 0)
        {
            String line = src[src.length - 1];
            int c = line.charAt(line.length() - 1);
            if(isLetter(c) == false)
                pos--;
        }
        if(src.length != 0)
            tokens.add(row, col, currentWord.toString(), src[row], pos);

    }

    public static boolean isLetter(char c)
    {
        int i = (int)c;
        return isLetter(i);
    }
    public static boolean isLetter(int  i){        

        if(i > 64 && i < 91)
            return true;
        if(i > 96 && i < 123)
            return true;
        return false;
    }
}
